<?php
require_once(dirname(__FILE__).'/../include/config.inc.php');
require_once "WxApi.php";
require_once "WxConfig.php";
require_once(dirname(__FILE__).'/../jssdk.php');


$appId=WxConfig::APPID;
$appSecret=WxConfig::APPSECRET;
$jssdk = new JSSDK($appId, $appSecret);
$signPackage = $jssdk->GetSignPackage();
$g_AccessToken=$jssdk->getAccessToken();


/*
if(isset($uid) && $uid>0 ){
		//是否存在该 openid
		$row = $dosql->GetOne("SELECT * FROM `#@__user` WHERE id='$uid'");
		$t_time=time();
		
		if(is_array($row) && isset($row['id'])){
			$openid_str=$row['openid'];
			if($row['nickname']==""){
				$getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$g_AccessToken."&openid=".$openid_str;
				$res2 = json_decode(httpGet($getUserInfoUrl),true);
				if($res2['subscribe']!=0){
						$nickname			=$res2['nickname'];
						$headimgurl		=$res2['headimgurl'];
						$sex						=$res2['sex'];
						$country 				=$res2['country'];
						$province			=$res2['province'];
						$city					=$res2['city'];
						$subscribe_time=$res2['subscribe_time'];   //最后一次关注时间
						var_dump($res2);
						$sql2="update `#@__user`  set nickname='$nickname',headimgurl='$headimgurl',sex='$sex',country='$country',province='$province',city='$city',subscribe_time='$subscribe_time'  where openid='$openid_str'  " ;
					$dosql->ExecNoneQuery($sql2);
				}
				
				
			}
			
}

}
*/
//https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID
$getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$g_AccessToken."&next_openid=";
$res2 = json_decode(httpGet($getUserInfoUrl),true);
$openids=$res2['data']['openid'];
foreach($openids as $val){
	if ($val) {
		//是否存在该 openid
		$row = $dosql->GetOne("SELECT * FROM `#@__user` WHERE openid='$val'");
		$t_time=time();
		
		if(is_array($row) && isset($row['id'])){
			// 存在的话 更新
				$getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$g_AccessToken."&openid=".$val;
				$res2 = json_decode(httpGet($getUserInfoUrl),true);
				if($res2['subscribe']!=0){
						$nickname			=$res2['nickname'];
						$headimgurl		=$res2['headimgurl'];
						$sex						=$res2['sex'];
						$country 				=$res2['country'];
						$province			=$res2['province'];
						$city					=$res2['city'];
						$subscribe_time=$res2['subscribe_time'];   //最后一次关注时间
						var_dump($res2);
						$sql2="update `#@__user`  set nickname='$nickname',headimgurl='$headimgurl',sex='$sex',country='$country',province='$province',city='$city',subscribe_time='$subscribe_time'  where openid='$val'  " ;
					$dosql->ExecNoneQuery($sql2);
					echo "更新完成";
				}
			
		}else{
			//不存在的话，直接创建
			if ($val) {
				$getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$g_AccessToken."&openid=".$val;
				$res2 = json_decode(httpGet($getUserInfoUrl),true);
				$userInfo=$res2;
			}
			//获取用户信息
			
			$nickname			=$res2['nickname'];
			$headimgurl		=$res2['headimgurl'];
			$sex						=$res2['sex'];
			$country 				=$res2['country'];
			$province			=$res2['province'];
			$city					=$res2['city'];
			$subscribe_time=$res2['subscribe_time'];   //最后一次关注时间
			//新建用户
			$sql2="insert into `#@__user`(openid,nickname,headimgurl,sex,country,province,city,regtime,subscribe_time,level) values ('$val','$nickname','$headimgurl','$sex','$country','$province','$city','$t_time','$subscribe_time','6')";
			$dosql->ExecNoneQuery($sql2);
			echo "创建完成";
		}
	}
}

function httpGet($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    // 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
    // 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2 );
	//curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');//这是根据http://curl.haxx.se/ca/cacert.pem 下载的证书，添加这句话之后就运行正常了
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
  }



//微信发送消息模板 函数

// json 为json格式的数据

function sendWxMsg($json){
	//发送消息
	global $g_AccessToken;
	if(isset($g_AccessToken)){
		$uuu="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$g_AccessToken;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $uuu);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //https协议
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
		$html = curl_exec($ch);
		$txt_name=dirname(__FILE__)."/txt/".time()."-sendWxMsg.txt";
		file_put_contents($txt_name,serialize($html));
	}

}






?>